import { Component, OnInit ,ViewChild } from '@angular/core';

import { CommonService } from "../../service/common.service";

import { Router,ActivatedRoute} from '@angular/router'; //导入router服务

@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})

export class RegisterPage implements OnInit {
  @ViewChild('iphone') myphone ;
  @ViewChild('iyzm') myyzm ;

  public phone:string = '';
  public yzm:string = '';
  public yqm:string =''
  // lfwy2
  public flagYzm:boolean = true;
  public times:any = 60;
  public sessionId:any='';
  public begin:any;
  public show:boolean=false;
  public ischeck:boolean=false
  public isclick:boolean=false
  

  constructor(public common:CommonService,private router: Router,private activatedRoute:ActivatedRoute
    ) {

  }

  // 初始化
  ngOnInit() {
    this.flagYzm = true;
    this.times = 60
  } 

  ionViewDidEnter(){
    this.flagYzm = true;
    this.times = 60
    this.yzm = ''
    window.clearInterval(this.begin)
  }

  // 协议是否为真
  ischeckbox(){
    console.log(this.ischeck)
    this.isClick()
  }
  
  // 验证码
  getYzm(){
    // 校验手机
    if(this.common.yzShouJi(this.phone) == false){
      return false
    }
    // 60s倒计时

    this.flagYzm = false
    this.times = 60;
    this.show=true
    this.begin = setInterval(()=>{   
        this.times--;
        if(this.times == 0){ 
            this.times = 60;
            this.flagYzm = true                     
            clearInterval(this.begin);
        }
    },1000)
    
    // 传参数
    var api = '/tbr/sendMessage'
    var params = {'phone': this.phone}
    
    this.common.post(api,params,true).then((response:any)=>{
     
      if(response.code == '200'){
        setTimeout(()=>{
          this.show=false
        },500)
        
        this.sessionId  = response.data.sessionId
        // console.log(response)
      }
      this.show=false
    })
  }

  //判断可点击
  isClick(){
    if(this.phone.length == 11){
      this.myphone.nativeElement.blur()
    }
    if(this.yzm.length == 4){
      this.myyzm.nativeElement.blur()
    }
    if(this.phone !== '' && this.yzm !== '' && this.yqm !== '' && this.ischeck == true){
      this.isclick=true
    }else{
      this.isclick=false
    }
  }

  // 注册
  register(){
    // 校验手机
    if(this.common.yzShouJi(this.phone) == false){
      return false
    }

    if(this.yzm == ''){
      this.common.presentAlert('验证码为空')
      return false
    }

    if(this.ischeck == false){
      this.common.presentAlert('请勾选协议')
      return false
    }
    
    this.show=true

     // 传参数
     var api = '/tbr/register'
     var params = {'phone': this.phone,'sessionId':this.sessionId,'invitationCode':this.yqm,'code':this.yzm,'deviceId':this.common._IsDevice()}
  
     this.common.post(api,params,true).then((response:any)=>{
      // console.log(response)
        if(response.code == 200){
          setTimeout(()=>{
            this.show=false
          },500)

          this.common.presentAlert("注册成功请登录") 
          //1、跳转到登录 (根)
          this.router.navigateByUrl("login")
        }else{
          this.common.presentAlert(response.msg) 
          setTimeout(()=>{
            this.show=false
          },500)
          this.flagYzm = true;
          clearInterval(this.begin);
        }
     })
  }

}
